package com.dxq.test;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.constant.PermissionConstants;
import com.blankj.utilcode.util.PermissionUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dxq.test.activity.AssetsActivity;
import com.dxq.test.adapter.FunctionAdapter;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class FunctionActivity extends AppCompatActivity {

    private SwipeMenuRecyclerView swipeMenuRecyclerView;

    private FunctionAdapter mAdapter;
    private List<String> mList = new ArrayList<>();

    FunctionActivity self = FunctionActivity.this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_function);
        initView();
        initSlide();  // 初始化 侧滑删除编辑
        initEvent();
        initData(20);
    }

    private void initView() {
        swipeMenuRecyclerView = (SwipeMenuRecyclerView) this.findViewById(R.id.swipeMenuRecyclerView);
        mAdapter = new FunctionAdapter(R.layout.function_item);
    }

    private void initEvent() {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (position) {
                    case 0:
                        startActivity(new Intent(self, AssetsActivity.class));
                        break;
                }
            }
        });
    }

    /**
     * 侧滑删除编辑
     */
    private void initSlide() {
        // srvWarn.setItemViewSwipeEnabled(true);// 开启滑动删除。默认关闭。
        swipeMenuRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        swipeMenuRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        // 创建菜单：
        SwipeMenuCreator mSwipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int viewType) {
           /* SwipeMenuItem deleteItem = new SwipeMenuItem(mContext);
            // 各种文字和图标属性设置。
            leftMenu.addMenuItem(deleteItem); // 在Item左侧添加一个菜单。*/


                // 在Item右侧添加一个菜单。
                // 1.编辑
                // 各种文字和图标属性设置。
                SwipeMenuItem modifyItem = new SwipeMenuItem(self)
                        .setBackgroundColor(getResources().getColor(R.color.colorAccent))
                        .setText("编辑")
                        .setTextColor(Color.BLACK)
                        .setTextSize(15) // 文字大小。
                        .setWidth(140)
                        .setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
                rightMenu.addMenuItem(modifyItem);
                // 2 删除
                SwipeMenuItem deleteItem = new SwipeMenuItem(self);
                deleteItem//.setText("删除")
                        .setBackgroundColor(getResources().getColor(R.color.colorPrimary))
                        //.setTextColor(Color.WHITE) // 文字颜色。
                        //.setTextSize(15) // 文字大小。
                        .setImage(R.drawable.ic_share_white_24dp)
                        .setWidth(140)
                        .setHeight(ViewGroup.LayoutParams.MATCH_PARENT);

                rightMenu.addMenuItem(deleteItem);

                // 注意：哪边不想要菜单，那么不要添加即可。
            }
        };
        // 设置监听器。
        swipeMenuRecyclerView.setSwipeMenuCreator(mSwipeMenuCreator);

        SwipeMenuItemClickListener mMenuItemClickListener = new SwipeMenuItemClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge) {
                // 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱。
                menuBridge.closeMenu();

                int direction = menuBridge.getDirection(); // 左侧还是右侧菜单。
                int adapterPosition = menuBridge.getAdapterPosition(); // RecyclerView的Item的position。
                int menuPosition = menuBridge.getPosition(); // 菜单在RecyclerView的Item中的Position。
                if (menuPosition == 0) {

                    //TallyRemindBean tallyRemindBean = tallyRecordList.get(adapterPosition);
                    //showTimeSelector(true, tallyRemindBean);

                } else {

                }
            }
        };

        // 菜单点击监听。
        swipeMenuRecyclerView.setSwipeMenuItemClickListener(mMenuItemClickListener);

        // 必须 最后执行
        swipeMenuRecyclerView.setAdapter(mAdapter);
    }

    private void initData(int num) {
        for (int i = 0; i < num; i++) {
            mList.add("Content_" + (1 + i));
        }
        mAdapter.addData(mList);
    }
}
